package com.chenxingnet.service.Impl;

import java.math.BigDecimal;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import com.chenxingnet.dao.AccountingControlMapper;
import com.chenxingnet.dao.DepartmentMapper;
import com.chenxingnet.dao.UsercontrolMapper;
import com.chenxingnet.dao.WaybillsMapper;
import com.chenxingnet.dto.Department;
import com.chenxingnet.dto.Waybills;
import com.chenxingnet.dto.WaybillsExample;
import com.chenxingnet.dto.vo.AccountingControlVo;
import com.chenxingnet.service.AccountingService;
import com.chenxingnet.tools.MobilePage;
import com.chenxingnet.tools.Page;
import com.chenxingnet.tools.PageKJ;
@Service("accountingService")
public class AccountingServiceImpl implements AccountingService{
	
	@Resource
	private AccountingControlMapper accountingControlMapper;
	
	@Resource
	private WaybillsMapper waybillsMapper;
	@Resource
	private DepartmentMapper deptDepartmentMapper;
	
	@Resource
	UsercontrolMapper  usercontrolMapper;

	public List<AccountingControlVo> selectAll(HttpServletRequest request, Model model) {
		String pageNow = request.getParameter("pageNow");
		PageKJ page = null;
		List<Waybills> list =new  ArrayList<Waybills>();
		int totalCount = (int) waybillsMapper.selectAll().size();
		
		if (pageNow != null) {
			page = new PageKJ(totalCount, Integer.parseInt(pageNow));
			list = this.waybillsMapper.getAllWaybillsMy(page.getStartPos(), page.getPageSize());
		} else {
			page = new PageKJ(totalCount, 1);
			list = this.waybillsMapper.getAllWaybillsMy(page.getStartPos(), page.getPageSize());
		}
		
		List<AccountingControlVo> aCVoList = new ArrayList<AccountingControlVo>();
		for (int i = 0; i < list.size(); i++) {
			AccountingControlVo aCVo = new AccountingControlVo();
			aCVo.setWaybid(list.get(i).getWid());
			aCVo.setDepid(list.get(i).getDeptid());
			//查询对应部门名字
			Department dept = deptDepartmentMapper.selectByPrimaryKey(list.get(i).getDeptid());
			aCVo.setDeptname(dept.getDeptname());
			aCVo.setComcity(list.get(i).getWcommence());
			aCVo.setMddcity(list.get(i).getWbourn());
			aCVo.setDriver(list.get(i).getWtransport());
			aCVo.setChexing(list.get(i).getWvehicle());
			aCVo.setQushu(list.get(i).getWactuate());
			aCVo.setSongda(list.get(i).getWdelivery());
			aCVo.setShigu(list.get(i).getWstate());
			aCVo.setChenum(list.get(i).getWvin());
			aCVo.setComTime(list.get(i).getWdeparturetime());
			aCVo.setDaoTime(list.get(i).getWestimatedtime());
			aCVo.setChangjia(list.get(i).getWfactory());
			aCVo.setJiashiyuan(list.get(i).getWpilot());
			aCVo.setBaoxian(list.get(i).getWcommercial().add(list.get(i).getWcoerce()));
			aCVo.setLinpai(list.get(i).getWamount());
			aCVo.setPingtiao(list.get(i).getWpicture());
			aCVo.setQiangxian(list.get(i).getWcoerce());
			aCVo.setJiesuan(list.get(i).getWtimeout());
			aCVo.setShangxian(list.get(i).getWcommercial());
			aCVo.setLirun(list.get(i).getWprofit());
			aCVo.setChangshangjiesuanshi(list.get(i).getWinsurance());
			//指令号
			aCVo.setZhiling(list.get(i).getWinstruct());
			
			try {
				String   uname=  usercontrolMapper.selectByPrimaryKey((list.get(i).getWdept())).getUname();
				aCVo.setDengjiren(uname);
			} catch (Exception e) {}
			aCVoList.add(aCVo);
		}
		model.addAttribute("page", page);
		request.setAttribute("page", page);
		return aCVoList;
	}

	/**
	 * 移动端分页查询
	 */
	public List<AccountingControlVo> mobSelectAll(HttpServletRequest request) {
		String pageNow = request.getParameter("pageNow");
		MobilePage mobilePage = null;
		List<Waybills> list =new  ArrayList<Waybills>();
		int totalCount = (int) waybillsMapper.selectAll().size();
		
		if (pageNow != null) {
			mobilePage = new MobilePage(totalCount, Integer.parseInt(pageNow));
			list = this.waybillsMapper.getAllWaybillsMy(mobilePage.getStartPos(), mobilePage.getPageSize());
		} else {
			mobilePage = new MobilePage(totalCount, 1);
			list = this.waybillsMapper.getAllWaybillsMy(mobilePage.getStartPos(), mobilePage.getPageSize());
		}
		
		List<AccountingControlVo> aCVoList = new ArrayList<AccountingControlVo>();
		for (int i = 0; i < list.size(); i++) {
			AccountingControlVo aCVo = new AccountingControlVo();
			aCVo.setWaybid(list.get(i).getWid());
			aCVo.setDepid(list.get(i).getDeptid());
			//查询对应部门名字
			Department dept = deptDepartmentMapper.selectByPrimaryKey(list.get(i).getDeptid());
			aCVo.setDeptname(dept.getDeptname());
			aCVo.setComcity(list.get(i).getWcommence());
			aCVo.setMddcity(list.get(i).getWbourn());
			aCVo.setDriver(list.get(i).getWtransport());
			aCVo.setChexing(list.get(i).getWvehicle());
			aCVo.setQushu(list.get(i).getWactuate());
			aCVo.setSongda(list.get(i).getWdelivery());
			aCVo.setShigu(list.get(i).getWstate());
			aCVo.setChenum(list.get(i).getWvin());
			aCVo.setComTime(list.get(i).getWdeparturetime());
			aCVo.setDaoTime(list.get(i).getWestimatedtime());
			aCVo.setChangjia(list.get(i).getWfactory());
			aCVo.setJiashiyuan(list.get(i).getWpilot());
			aCVo.setBaoxian(list.get(i).getWcommercial().add(list.get(i).getWcoerce()));
			aCVo.setLinpai(list.get(i).getWamount());
			aCVo.setPingtiao(list.get(i).getWpicture());
			aCVo.setQiangxian(list.get(i).getWcoerce());
			aCVo.setJiesuan(list.get(i).getWtimeout());
			aCVo.setShangxian(list.get(i).getWcommercial());
			aCVo.setLirun(list.get(i).getWprofit());
			aCVo.setChangshangjiesuanshi(list.get(i).getWinsurance());
			//指令号
			aCVo.setZhiling(list.get(i).getWinstruct());
			
			try {
				String   uname=  usercontrolMapper.selectByPrimaryKey((list.get(i).getWdept())).getUname();
				aCVo.setDengjiren(uname);
			} catch (Exception e) {}
			aCVoList.add(aCVo);
		}
		request.setAttribute("mobilePage", mobilePage);
		return aCVoList;
	}
	
	public List<AccountingControlVo> selectByDeptVSveh(Integer deptid,
			String veh) {
		
		return null;
	}

	public  static   String  panDuan(String    teata){
		if(teata.equals("选择")){
			teata="";
		}
		return   teata;
	}
	
	public List<AccountingControlVo>   selectAllBySerach(HttpServletRequest request, Model model,String deptname,String chexing,String   xingming,  String  jiesuan,String  jiashiyuanjs) {
		String pageNow = request.getParameter("pageNow");
		Page page = null;
		WaybillsExample   querWay=new WaybillsExample();//利用example按照条件动态查询，这样就不用自己写查询代码了。但是无法分页
		if(!deptname.equals("选择")){
			if(deptname.equals("成都")){
				querWay.createCriteria().andDeptidEqualTo(1);//按照条件进行赋值，createCriteria方法是固定的，andDeptidEqualTo  的意思为并且部门id等于XXX
			}
			if(deptname.equals("绵阳")){
				querWay.createCriteria().andDeptidEqualTo(2);
			}
		}
		if(!chexing.equals("选择")){
			/*querWay.createCriteria().andWvinEqualTo(chexing);*/
			querWay.createCriteria().andWvehicleEqualTo(chexing);
		}
		if(!xingming.equals("选择")){
			querWay.createCriteria().andWtransportEqualTo(xingming);
		}
		if(!jiashiyuanjs.equals("选择")){
			querWay.createCriteria().andWtimeoutEqualTo(jiashiyuanjs);
		}
		
		if(!jiesuan.equals("选择")){
			if(jiesuan.equals("已结算")){
				querWay.createCriteria().andWinsuranceEqualTo(new  BigDecimal(2));
			}
			if(jiesuan.equals("未结算")){
				querWay.createCriteria().andWinsuranceEqualTo(new  BigDecimal(1));
			}
		}
		
		List<Waybills>  list    =  waybillsMapper.selectByExample(querWay);//利用example按照条件动态查询，这样就不用自己写查询代码了。但是无法分页
		List<Waybills>  listTmp    = new  ArrayList<Waybills>();//建立临时的运单listTmp，最后给list复制返回数据
	//	List<Waybills> list=waybillsMapper.selectAllForSerach( deptname, chexing,   xingming,    jiesuan);
		int totalCount = (int) list.size();
		
		if (pageNow != null) {//进行分页，将分页的两个参数利用for循环进行筛选，
			page = new Page(totalCount, Integer.parseInt(pageNow));
			//list = this.waybillsMapper.selectByExamplehByPage( querWay,page.getStartPos(), page.getPageSize());
			int   indextm=0;
			for(int a=0;a<list.size();a++){
				if(a>=page.getStartPos()&&indextm<page.getPageSize()){//分页的两个参数进行筛选
					listTmp.add(list.get(a));
					indextm=indextm+1;
				}
			}
			list=new  ArrayList<Waybills>();
			
			//list.addAll(listTmp);
			for(int ab=listTmp.size()-1;ab>=0;ab-- ){
				list.add(listTmp.get(ab));
			}
		} else {
			page = new Page(totalCount, 1);
			//list = this.waybillsMapper.selectByExamplehByPage( querWay,page.getStartPos(), page.getPageSize());
			int   indextm=0;
			for(int a=0;a<list.size();a++){
				if(a>=page.getStartPos()&&indextm<page.getPageSize()){//分页的两个参数进行筛选
					listTmp.add(list.get(a));
					indextm=indextm+1;
				}
			}
			list=new  ArrayList<Waybills>();
			//list.addAll(listTmp);
			for(int ab=listTmp.size()-1;ab>=0;ab-- ){
				list.add(listTmp.get(ab));
			}
		}
		
		List<AccountingControlVo> aCVoList = new ArrayList<AccountingControlVo>();
		
		for (int i = 0; i < list.size(); i++) {
			AccountingControlVo aCVo = new AccountingControlVo();
			aCVo.setWaybid(list.get(i).getWid());
			aCVo.setDepid(list.get(i).getDeptid());
			//查询对应部门名字
			Department dept = deptDepartmentMapper.selectByPrimaryKey(list.get(i).getDeptid());
			aCVo.setDeptname(dept.getDeptname());
			aCVo.setComcity(list.get(i).getWcommence());
			aCVo.setMddcity(list.get(i).getWbourn());
			aCVo.setDriver(list.get(i).getWtransport());
			aCVo.setChexing(list.get(i).getWvehicle());
			aCVo.setQushu(list.get(i).getWactuate());
			aCVo.setSongda(list.get(i).getWdelivery());
			aCVo.setShigu(list.get(i).getWstate());
			aCVo.setChenum(list.get(i).getWvin());
			aCVo.setComTime(list.get(i).getWdeparturetime());
			aCVo.setDaoTime(list.get(i).getWestimatedtime());
			aCVo.setChangjia(list.get(i).getWfactory());
			aCVo.setJiashiyuan(list.get(i).getWpilot());
			//保险=商业险+强险list.get(i).getWinsurance()
			/*aCVo.setBaoxian(list.get(i).getWcommercial().add(list.get(i).getWcoerce()));*/
			aCVo.setLinpai(list.get(i).getWamount());
			aCVo.setJiesuan(list.get(i).getWtimeout());
			aCVo.setPingtiao(list.get(i).getWpicture());
			aCVo.setChangshangjiesuanshi(list.get(i).getWinsurance());
			try {
				String   uname=  usercontrolMapper.selectByPrimaryKey((list.get(i).getWdept())).getUname();
				aCVo.setDengjiren(uname);
			} catch (Exception e) {}
			aCVo.setQiangxian(list.get(i).getWcoerce());
			aCVo.setShangxian(list.get(i).getWcommercial());
			aCVo.setLirun(list.get(i).getWprofit());
			//指令号
			aCVo.setZhiling(list.get(i).getWinstruct());
			
			aCVoList.add(aCVo);
		}
		model.addAttribute("page", page);
		return aCVoList;
	}



	public List<AccountingControlVo> selectAllSingle(int parseInt) {
		List<AccountingControlVo> aCVoList = new ArrayList<AccountingControlVo>();
		Waybills    wy= waybillsMapper.selectByPrimaryKey(parseInt);
		List<Waybills> list =new  ArrayList<Waybills>();
		list.add(wy);
		for (int i = 0; i < list.size(); i++) {
			AccountingControlVo aCVo = new AccountingControlVo();
			aCVo.setWaybid(list.get(i).getWid());
			aCVo.setDepid(list.get(i).getDeptid());
			//查询对应部门名字
			Department dept = deptDepartmentMapper.selectByPrimaryKey(list.get(i).getDeptid());
			aCVo.setDeptname(dept.getDeptname());
			aCVo.setComcity(list.get(i).getWcommence());
			aCVo.setMddcity(list.get(i).getWbourn());
			aCVo.setDriver(list.get(i).getWtransport());
			aCVo.setChexing(list.get(i).getWvehicle());
			aCVo.setQushu(list.get(i).getWactuate());
			aCVo.setSongda(list.get(i).getWdelivery());
			aCVo.setShigu(list.get(i).getWstate());
			aCVo.setChenum(list.get(i).getWvin());
			aCVo.setComTime(list.get(i).getWdeparturetime());
			aCVo.setDaoTime(list.get(i).getWestimatedtime());
			aCVo.setChangjia(list.get(i).getWfactory());
			aCVo.setJiashiyuan(list.get(i).getWpilot());
			aCVo.setBaoxian(list.get(i).getWcommercial().add(list.get(i).getWcoerce()));
			aCVo.setLinpai(list.get(i).getWamount());
			aCVo.setPingtiao(list.get(i).getWpicture());
			aCVo.setQiangxian(list.get(i).getWcoerce());
			aCVo.setChangshangjiesuanshi(list.get(i).getWinsurance());
			try {
				String   uname=  usercontrolMapper.selectByPrimaryKey((list.get(i).getWdept())).getUname();
				aCVo.setDengjiren(uname);
			} catch (Exception e) {}
			aCVo.setJiesuan(list.get(i).getWtimeout());
			aCVo.setShangxian(list.get(i).getWcommercial());
			aCVo.setLirun(list.get(i).getWprofit());
			//指令号
			aCVo.setZhiling(list.get(i).getWinstruct());
			
			aCVoList.add(aCVo);
		}
		
		return aCVoList;
	}

	public List<AccountingControlVo> selectAllBySou(HttpServletRequest request,
			Model model, Waybills waybills) {
		String pageNow = request.getParameter("pageNow");
		Page page = null;
		List<Waybills>  list = null;
		try {
			list = waybillsMapper.selectList(waybills);
		} catch (Exception e) {
			System.out.println("selectAllBySou");
			e.printStackTrace();
		}
		System.out.println(list.size());
		List<Waybills>  listTmp    = new  ArrayList<Waybills>();//建立临时的运单listTmp，最后给list复制返回数据
		int totalCount = (int) list.size();
		
		if (pageNow != null) {//进行分页，将分页的两个参数利用for循环进行筛选，
			page = new Page(totalCount, Integer.parseInt(pageNow));
			int   indextm=0;
			for(int a=0;a<list.size();a++){
				if(a>=page.getStartPos()&&indextm<page.getPageSize()){//分页的两个参数进行筛选
					listTmp.add(list.get(a));
					indextm=indextm+1;
				}
			}
			list=new  ArrayList<Waybills>();
			
			//list.addAll(listTmp);
			for(int ab=listTmp.size()-1;ab>=0;ab-- ){
				list.add(listTmp.get(ab));
			}
		} else {
			page = new Page(totalCount, 1);
			//list = this.waybillsMapper.selectByExamplehByPage( querWay,page.getStartPos(), page.getPageSize());
			int   indextm=0;
			for(int a=0;a<list.size();a++){
				if(a>=page.getStartPos()&&indextm<page.getPageSize()){//分页的两个参数进行筛选
					listTmp.add(list.get(a));
					indextm=indextm+1;
				}
			}
			list=new  ArrayList<Waybills>();
			//list.addAll(listTmp);
			for(int ab=listTmp.size()-1;ab>=0;ab-- ){
				list.add(listTmp.get(ab));
			}
		}
		
		List<AccountingControlVo> aCVoList = new ArrayList<AccountingControlVo>();
		
		for (int i = 0; i < list.size(); i++) {
			AccountingControlVo aCVo = new AccountingControlVo();
			aCVo.setWaybid(list.get(i).getWid());
			aCVo.setDepid(list.get(i).getDeptid());
			//查询对应部门名字
			Department dept = deptDepartmentMapper.selectByPrimaryKey(list.get(i).getDeptid());
			aCVo.setDeptname(dept.getDeptname());
			aCVo.setComcity(list.get(i).getWcommence());
			aCVo.setMddcity(list.get(i).getWbourn());
			aCVo.setDriver(list.get(i).getWtransport());
			aCVo.setChexing(list.get(i).getWvehicle());
			aCVo.setQushu(list.get(i).getWactuate());
			aCVo.setSongda(list.get(i).getWdelivery());
			aCVo.setShigu(list.get(i).getWstate());
			aCVo.setChenum(list.get(i).getWvin());
			aCVo.setComTime(list.get(i).getWdeparturetime());
			aCVo.setDaoTime(list.get(i).getWestimatedtime());
			aCVo.setChangjia(list.get(i).getWfactory());
			aCVo.setJiashiyuan(list.get(i).getWpilot());
			//保险=商业险+强险list.get(i).getWinsurance()
			/*aCVo.setBaoxian(list.get(i).getWcommercial().add(list.get(i).getWcoerce()));*/
			aCVo.setLinpai(list.get(i).getWamount());
			aCVo.setJiesuan(list.get(i).getWtimeout());
			aCVo.setPingtiao(list.get(i).getWpicture());
			aCVo.setChangshangjiesuanshi(list.get(i).getWinsurance());
			try {
				String   uname=  usercontrolMapper.selectByPrimaryKey((list.get(i).getWdept())).getUname();
				aCVo.setDengjiren(uname);
			} catch (Exception e) {}
			aCVo.setQiangxian(list.get(i).getWcoerce());
			aCVo.setShangxian(list.get(i).getWcommercial());
			aCVo.setLirun(list.get(i).getWprofit());
			//指令号
			aCVo.setZhiling(list.get(i).getWinstruct());
			aCVoList.add(aCVo);
		}
		model.addAttribute("page", page);
		return aCVoList;
	}
	
	
	

}
